﻿CREATE PROCEDURE [dbo].[ArticleSales_InsertFromTempArticleSales]
AS
	INSERT INTO ArticleSales(StoreId, ArticleId, BonusBuyNumber, Date, Units, Sales, GM, GrossSales, Price, Confirmed)
	SELECT distinct StoreId, ArticleId, BonusBuyNumber, Date, Units, Sales, GM, GrossSales, Price, 1
	FROM TempArticleSales temp
	WHERE 
	temp.Sales > 0 and temp.Units > 0
	AND
	NOT EXISTS
		(select * 
		 from ArticleSales sales
		 where 
			   sales.StoreId = temp.StoreId and
			   sales.Date = temp.Date and			   
			   sales.ArticleId = temp.ArticleId and
			   sales.BonusBuyNumber = temp.BonusBuyNumber)